<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Config.php
 * Created on : 2021-1-31 15:11:28
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\admin\controller;

use app\Request;
use haoteam\service\UtilService;
use app\admin\model\AdminConfigTab;
use app\admin\model\AdminConfig;
use haoteam\http\validates\AdminConfigValidate;

/**
 * 系统配置管理
 * Class Config
 * Tiger <1192851302@qq.com> 2021-1-31 15:11:28
 */
class Config {

    /**
     * 配置分组管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function tab() {
        $post = UtilService::postMore([
                    'status',
                    'title'
        ]);
        return app('json')->successful(AdminConfigTab::getList($post));
    }

    /**
     * 配置分组添加
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function tab_create() {
        $post = UtilService::postMore([
                    'status',
                    'title'
        ]);
        if (empty($post['title'])) {
            return app('json')->fail('名称不能为空');
        }
        try {
            AdminConfigTab::create($post);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('添加成功');
    }

    /**
     * 配置分组更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function tab_update() {
        $post = UtilService::postMore([
                    'id',
                    'status',
                    'title'
        ]);
        if (empty($post['title'])) {
            return app('json')->fail('名称不能为空');
        }
        if (empty($post['id'])) {
            return app('json')->fail('ID不能为空');
        }
        try {
            AdminConfigTab::update($post, ['id' => $post['id']]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 配置分组删除
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function tab_delete() {
        $post = UtilService::postMore([
                    'id'
        ]);
        if (empty($post['id'])) {
            return app('json')->fail('ID不能为空');
        }
        try {
            AdminConfigTab::update(['is_del' => 1], ['id' => $post]);
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

    /**
     * 配置参数类型
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function get_type() {
        return app('json')->successful(AdminConfig::$typeList);
    }

    /**
     * 配置分组列表
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function get_tab() {
        return app('json')->successful(AdminConfigTab::getList());
    }

    /**
     * 配置参数管理
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function index() {
        $post = UtilService::postMore([
                    'status',
                    'title',
                    'tab_id'
        ]);
        return app('json')->successful(AdminConfig::getList($post));
    }

    /**
     * 获取配置参数
     * @login true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function get() {
        $post = UtilService::postMore([
                    'tab_id',
                    ['status', 1]
        ]);
        return app('json')->successful(AdminConfig::getList($post));
    }

    /**
     * 配置参数添加
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function create() {
        $post = UtilService::postMore([
                    'type',
                    'tab_id',
                    'title',
                    'name',
                    'rules',
                    'descs',
                    'sort',
                    'params',
                    'status'
        ]);
        $validate = new AdminConfigValidate();
        if ($validate->scene('create')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminConfig::create($post);
        } catch (Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('添加成功');
    }

    /**
     * 单字段更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @param Request $request
     * @return type
     */
    public function field_update(Request $request) {
        [$id, $field, $value] = UtilService::postMore([
                    'id',
                    'field',
                    'value'
                        ], $request, true);
        try {
            AdminConfig::update(["{$field}" => $value], ['id' => $id]);
        } catch (Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 配置参数更新
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function update() {
        $post = UtilService::postMore([
                    'id',
                    'type',
                    'tab_id',
                    'title',
                    'name',
                    'rules',
                    'descs',
                    'sort',
                    'params',
                    'status'
        ]);
        $validate = new AdminConfigValidate();
        if ($validate->scene('update')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminConfig::update($post, ['id' => $post['id']]);
        } catch (Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('更新成功');
    }

    /**
     * 配置参数删除
     * @auth true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function delete() {
        $post = UtilService::postMore([
                    'id'
        ]);
        $validate = new AdminConfigValidate();
        if ($validate->scene('delete')->check($post) == false) {
            return app('json')->fail($validate->getError());
        }
        try {
            AdminConfig::destroy(['id' => $post['id']]);
        } catch (Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('删除成功');
    }

    /**
     * 系统参数设置
     * @auth true
     * @menu true
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public function set() {
        $post = UtilService::postMore([
                    ['data', []]
        ]);
        try {
            foreach ($post['data'] as $key => $value) {
                AdminConfig::setValue($key, $value);
            }
        } catch (\Exception $exc) {
            return app('json')->fail($exc->getMessage());
        }
        return app('json')->successful('设置成功');
    }

}
